//
//  LECheckStaffController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/18.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LECheckStaffController.h"
#import "LEStaffCell.h"
#import "LEStaffModel.h"

#import "LEStaffDetialController.h"

static NSString *const LEStaffCellId = @"LEStaffCell";

@interface LECheckStaffController ()

@end

@implementation LECheckStaffController

- (void)setupUI
{
    [super setupUI];
    
    self.view.backgroundColor = LEHaxColor(0xf0f0f0);
    self.tableView.rowHeight = 100;
    [self.tableView registerNib:[UINib nibWithNibName:@"LEStaffCell" bundle:nil] forCellReuseIdentifier:LEStaffCellId];
    [self initHeader];
    [self initFooter];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    [dict setObject:@"10" forKey:@"relation_verify"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StaffListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];

            NSArray *serviceList = datas[@"service_list"];
            NSArray *tempModel = [LEStaffModel mj_objectArrayWithKeyValuesArray:serviceList];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

/**
 审核员工

 @param tag <#tag description#>
 @param model <#model description#>
 */
- (void)requestChectStaff:(NSInteger)tag staff:(LEStaffModel *)model
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:model.relation_id forKey:@"relation_id"];
    if (tag == 1)
    {
        [dict setObject:@"1" forKey:@"relation_verify"];
    }else if (tag == 2)
    {
        [dict setObject:@"0" forKey:@"relation_verify"];
    }
    
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:StaffChangeRolelUrl params:dict success:^(NSDictionary *successCode) {
        
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:datas];
            self.page = 1;
            [weakSelf requestRefreshData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];

}
#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStaffCell *cell = [tableView dequeueReusableCellWithIdentifier:LEStaffCellId forIndexPath:indexPath];
    [cell setCheckModel:self.dataSource[indexPath.row]];
     __weak typeof (self) weakSelf = self;
    [cell staffCellClick:^(NSInteger tag, LEStaffModel *model) {
        [weakSelf requestChectStaff:tag staff:model];
    }];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEStaffDetialController *staffDetail = [[LEStaffDetialController alloc] init];
    LEStaffModel *model = self.dataSource[indexPath.row];
    staffDetail.store_id = model.store_id;
    staffDetail.service_id = model.service_id;
    [self.navigationController pushViewController:staffDetail animated:YES];
}


@end
